/* Create parties with most spending, events, leaders, volunteers, secret spending?*/

use "Raw Data/journalist_survey.dta", clear
merge m:1 state_name pc_num using "Analysis Data/PC_est_sample.dta", keep(matched) nogen

*Most Spending*
preserve
reshape long spend_, i(state_name pc_num jcode)
rename spend_ party_name
drop if party_name == ""
bys state_name pc_num party_name: drop if _n > 1
keep state_name pc_num party_name
save "Analysis Data/spend.dta", replace
restore

*Most Events*
preserve
reshape long events_, i(state_name pc_num jcode)
rename events_ party_name
drop if party_name == ""
bys state_name pc_num party_name: drop if _n > 1
keep state_name pc_num party_name
save "Analysis Data/events.dta", replace
restore

*Most Events*
preserve
reshape long leaders_, i(state_name pc_num jcode)
rename leaders_ party_name
drop if party_name == ""
bys state_name pc_num party_name: drop if _n > 1
keep state_name pc_num party_name
save "Analysis Data/leaders.dta", replace
restore

*Volunteers*
preserve
reshape long volunteers_, i(state_name pc_num jcode)
rename volunteers_ party_name
drop if party_name == ""
bys state_name pc_num party_name: drop if _n > 1
keep state_name pc_num party_name
save "Analysis Data/volunteers.dta", replace
restore

*Win*
preserve
reshape long win_, i(state_name pc_num jcode)
rename win_ party_name
drop if party_name == ""
bys state_name pc_num party_name: drop if _n > 1
keep state_name pc_num party_name
save "Analysis Data/win.dta", replace
restore

*Vote-buying*
preserve
drop if secret_1==""
bys state_name pc_num: egen num_resp = count(jcode)
keep state_name pc_num num_resp
bys state_name pc_num: drop if _n>1
tempfile num_resp
save `num_resp'
restore 

drop if secret_1==""
gen num_parties = 3 if secret_3 != ""
replace num_parties = 2 if secret_3 == ""
replace num_parties = 1 if secret_2 == ""

reshape long secret_, i(state_name pc_num jcode)
rename secret_ party_name
drop if party_name == ""

	*Level of consensus
	preserve
	collapse (count) num_report=_j, by(state_name pc_num party_name)
	merge m:1 state_name pc_num using `num_resp', keepusing(num_resp) nogen
	encode party_name, gen(party)
	drop party_name
	reshape wide num_report, i(state_name pc_num) j(party)
	recode num_report* (.=-9)
	egen max_report = rmax(num_report*)
	restore
	
merge m:1 state_name pc_num using `num_resp', keepusing(num_resp) nogen

bys state_name pc_num party_name: drop if _n > 1
keep state_name pc_num party_name num_resp
save "Analysis Data/votebuying.dta", replace

